package com.sc.test.dagger2;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.sc.test.R;
import com.sc.test.app.App;
import com.sc.test.utils.Util;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao.
 * Created on 2018-06-02-002.
 */

public class Dagger2Activity extends AppCompatActivity {
    @Inject
    Cloth cloth;
    @Inject
    @RedCloth
    Cloth redCloth;
    @Inject
    @Named("blue")
    Cloth blueCloth;
    @BindView(R.id.tv_dagger)
    TextView tvDagger;
    @Inject
    Shoe shoe;
    @Inject
    Clothes clothes;
    @Inject
    ClothHandler clothHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dagger2);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
//        DaggerDagger2Component.create().inject(this);
//        DaggerDagger2Component.builder().dagger2Module(new Dagger2Module()).build();
//        Dagger2Component build = DaggerDagger2Component
//                .builder()
//                .baseComponent(((App)getApplication()).getBaseComponent())
//                .dagger2Module(new Dagger2Module())
//                .build();
//        build.inject(this);
        App application = (App) getApplication();
        application.getBaseComponent().getSubDagger2Component(new Dagger2Module()).inject(this);
        tvDagger.setText(cloth.getColor() + shoe.toString() + clothes);
        tvDagger.append("\n");
        tvDagger.append(redCloth.getColor() + "**********" + blueCloth.getColor());
        boolean b = blueCloth == clothes.getCloth();
        tvDagger.append("\n");
        tvDagger.append(b + "");
    }

    @OnClick({R.id.btn_execute, R.id.btn_second})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_execute:
                tvDagger.setText("红颜料：" + clothHandler.handle(redCloth) + ":" + clothHandler);
                String userAgent = System.getProperty("http.agent");
                tvDagger.append("\n");
                tvDagger.append(userAgent);
                break;
            case R.id.btn_second:
                Util.openNewActivity(this, SecondActivity.class, "");
                break;
        }
    }
}
